package cn.yunhe.spring.redis;

import cn.yunhe.spring.mybatis.model.User;
import cn.yunhe.spring.mybatis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/11.
 */
@Service
public class StartAddCacheListner implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private RedisCacheUtil<Object> redisCache;
    @Autowired
    private IUserService userService;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event)
    {
        //spring 启动的时候缓存城市和国家等信息
        if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext"))
        {
            System.out.println("\n\n\n_________\n\n缓存数据 \n\n ________\n\n\n\n");
            //访问userService里面的方法
            List<User> userList =userService.findAllUsers();
           /* List<Country> countryList = brandStoreService.selectAllCountryMessage();*/

            Map<Integer,User> userMap = new HashMap<Integer,User>();

           /* Map<Integer,Country> countryMap = new HashMap<Integer, Country>();*/
            //得到uesrList的大小
            int userListSize = userList.size();

            for(int i = 0 ; i < userListSize ; i ++ )
            {
                userMap.put(userList.get(i).getId(), userList.get(i));
            }

           /* for(int i = 0 ; i < countryListSize ; i ++ )
            {
                countryMap.put(countryList.get(i).getCountry_id(), countryList.get(i));
            }*/

            redisCache.setCacheIntegerMap("userMap", userMap);
           /* redisCache.setCacheIntegerMap("countryMap", countryMap);*/
        }
    }
}
